package org.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test2 {
    //leetcode 子集II https://leetcode.cn/problems/subsets-ii/description/
    public List<List<Integer>> ret;
    List<Integer> path;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        Arrays.sort(nums);
        bachtracking(nums,0);
        return ret;
    }

    public void bachtracking(int[] nums, int start) {
        ret.add(new ArrayList<>(path));
        int prev = -11;
        for (int i = start; i < nums.length; i++) {
            if (nums[i] == prev) continue;
            path.add(nums[i]);
            prev = nums[i];
            bachtracking(nums,i + 1);
            path.remove(path.size() - 1);
        }
    }
}
